package com.mj;

/**
 * 双向链表
 *
 * @param <E>
 */
public class LinkedList<E> extends AbstractList<E> {

    private Node<E> first;
    private Node<E> last;

    private static class Node<E> {
        E element;
        Node<E> pre;
        Node<E> next;

        public Node(Node<E> pre, E element, Node<E> next) {
            this.pre = pre;
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();

            if (pre != null) sb.append(pre.element);
            else sb.append("NULL");

            sb.append("_").append(element).append("_");

            if (next != null) sb.append(next.element);
            else sb.append("NULL");

            return sb.toString();
        }
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    /**
     * 获取节点中的数据
     *
     * @param index
     * @return
     */
    @Override
    public E get(int index) {

        return getNode(index).element;
    }

    /**
     * 先把之前的元素取出来,在覆盖
     *
     * @param index
     * @param element
     * @return
     */
    @Override
    public E set(int index, E element) {
        Node<E> oldNode = getNode(index);
        E oldElement = oldNode.element;
        oldNode.element = element;
        return oldElement;
    }

    public E getFirst() {

        if (first != null) return first.element;

        return null;
    }

    public void addFirst(E element) {
        Node<E> f = first;
        Node<E> node = new Node<>(null, element, f);
        first = node;
        if (f == null) last = node;
        else f.pre = node;
        size++;
    }

    public E removeLast(){
        Node<E> node = last;

        Node<E> pre = node.pre;
        last = pre;
        if (pre == null) first = null;
        else pre.next = null;

        size--;
        return node.element;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        //index==0 size ==0
        if (index == size) {
            Node<E> pre = last;
            //==>Node<E> pre1 = getNode(index); 减少调用和计算
            last = new Node<>(pre, element, null);
            if (pre == null) {//链表添加第一个元素
                first = last;
            } else {
                pre.next = last;
            }
        } else {
            Node<E> next = getNode(index);
            Node<E> pre = next.pre;

            Node<E> newNode = new Node<>(pre, element, next);
            next.pre = newNode;
            if (pre == null)//index==0
            {
                first = newNode;
            } else {
                pre.next = newNode;
            }
        }
        size++;
    }

    /**
     * 获取索引所在的节点
     *
     * @param index
     * @return
     */
    private Node<E> getNode(int index) {
        rangeCheck(index);
        if (index < (size >> 1)) {
            Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node<E> node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.pre;
            }
            return node;
        }
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);

        Node<E> node = getNode(index);

        Node<E> next = node.next;
        Node<E> pre = node.pre;

        if (pre == null) {//index==0
            first = next;
        } else {
            pre.next = next;
        }
        if (next == null) {//index = size - 1
            last = pre;
        } else {
            next.pre = pre;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;

                node = node.next;
            }
        } else {
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (element.equals(node.element)) return i;

                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }


    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        Node<E> node = this.first;
        string.append("size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            string.append(node);
            node = node.next;
            if (i != size - 1) {
                string.append(", ");
            }
        }
        string.append("]");
        return string.toString();
    }
}
